
public class SinglyLinkedList {
	public class Node{
		private int value;
		private Node next;
		
		public Node(int value){
			this.value = value;
			this.next = null;
		}

		public int getValue() {
			return value;
		}

		public void setValue(int value) {
			this.value = value;
		}

		public Node getNext() {
			return next;
		}

		public void setNext(Node next) {
			this.next = next;
		}
	}
	
	protected Node start;
	
	public SinglyLinkedList(){
		start = null;
	}
	
	public void addFirst(int value){
		if(start == null){
			start = new Node(value);
			return;
		}
		Node p = new Node(value);
		p.next = start;
		start = p;
	}
	
	public void removeFirst(){
		if(start == null) return;
		start = start.next;
	}
	
	public void addTail(int value){
		if(start == null){
			start = new Node(value);
			return;
		}
		Node run = start;
		while(run.next != null) run = run.next;
		Node p = new Node(value);
		run.next = p;
	}
	
	public void removeTail(){
		if(start == null) return;
		if(start.next == null) start = null;
		else{
			Node run = start;
			while(run.next.next != null) run = run.next;
			run.next = null;
		}
	}
	
	public void insertAfter(int pos, int value){
		Node run = start;
		int count = 1;
		while(count < pos){
			run = run.next;
			count++;
		}
		Node p = new Node(value);
		p.next = run.next;
		run.next = p;
	}
	
	public void printList(){
		if(start == null) return;
		Node run = start;
		while(run != null){
			System.out.print(run.value + " ");
			run = run.next;
		}
		System.out.println();
	}
}
